package com.atguigu.day01;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<String> stringDataStreamSource = env.socketTextStream("hadoop102",65535);
        SingleOutputStreamOperator<WordWithCount> mapped = stringDataStreamSource.flatMap(new FlatMapFunction<String, WordWithCount>() {
            @Override
            public void flatMap(String line, Collector<WordWithCount> out) throws Exception {
                String[] words = line.split(" ");
                for (String word : words) {
                    out.collect(new WordWithCount(word, 1L));
                }
            }
        });
        KeyedStream<WordWithCount, String> keyedStream = mapped.keyBy(new KeySelector<WordWithCount, String>() {
            @Override
            public String getKey(WordWithCount wordWithCount) throws Exception {
                return wordWithCount.word;
            }
        });
        SingleOutputStreamOperator<WordWithCount> reduce = keyedStream.reduce(new ReduceFunction<WordWithCount>() {
            @Override
            public WordWithCount reduce(WordWithCount wordWithCount, WordWithCount t1) throws Exception {
                return new WordWithCount(wordWithCount.word,wordWithCount.value+t1.value);
            }
        });
        reduce.print();
        env.execute();
    }

    public static class WordWithCount {
        public String word;
        public Long value;

        public WordWithCount(String word, Long value) {
            this.word = word;
            this.value = value;
        }

        @Override
        public String toString() {
            return "WordWithCount{" +
                    "word='" + word + '\'' +
                    ", value=" + value +
                    '}';
        }
    }
}
